package com.jinzhi.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.common.utils.DateUtil;
import com.jinzhi.jzweb.domain.ExaminationBatchComDO;
import com.jinzhi.jzweb.domain.ItemBankDO;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.service.ExaminationBatchComService;
import com.jinzhi.jzweb.service.ItemBankService;
import com.jinzhi.jzweb.service.StuSignUpComService;
import com.jinzhi.sys.service.HomeStatisticsService;
import com.jinzhi.sys.vo.HomeStatisticsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/*
 *  @author : JingChangTian
 *  @date   : 2023/8/2 15:51
 *  @description :
 *
 */
@Service
public class HomeStatisticsServiceImpl implements HomeStatisticsService {

    /**
     * 竞赛批次
     */
    private final ExaminationBatchComService examinationBatchComService;

    /**
     * 考试试题
     */
    private final ItemBankService itemBankService;

    /**
     * 报名考生
     */
    private final StuSignUpComService stuSignUpComService;

    public HomeStatisticsServiceImpl(ExaminationBatchComService examinationBatchComService, ItemBankService itemBankService, StuSignUpComService stuSignUpComService) {
        this.examinationBatchComService = examinationBatchComService;
        this.itemBankService = itemBankService;
        this.stuSignUpComService = stuSignUpComService;
    }

    @Override
    public HomeStatisticsVo getOrderRecharge() {
        //获取总竞赛数
        Integer incomeCompetition = Optional.of(
                examinationBatchComService.selectList(
                        new EntityWrapper<ExaminationBatchComDO>().eq("type",3)).size()).
                orElse(0);
        //获取总试题数
        Integer topicCount = Optional.of(
                itemBankService.selectList(new EntityWrapper<ItemBankDO>()).size()).
                orElse(0);
        //获取总考生数
        List<StuSignUpComDO> stus = Optional.of(stuSignUpComService.selectList(new EntityWrapper<StuSignUpComDO>())).orElse(new ArrayList<>());
        //考生总人数
        Integer totality = stus.size();
        //参加考生数
        Integer headcount =  Optional.of(Math.toIntExact(stus.stream().
                filter(a -> StringUtils.isNotBlank(a.getNum1())).
                filter(a -> StringUtils.isNotBlank(a.getRealityNm1())).
                count())).orElse(0);
        //昨日参加
        Integer yesterdaySum = Optional.of(Math.toIntExact(stus.stream().
                filter(a-> DateUtil.getDateFormat(a.getSignUpTime(),"yyyy-MM-dd").
                        equals(LocalDate.now().minusDays(1).toString())).
                count())).orElse(0);
        //最近一周参加
        Integer weeksSum = Optional.of(Math.toIntExact(stus.stream().
                filter(a-> Integer.parseInt(String.valueOf(LocalDate.now().getDayOfMonth())) - Integer.parseInt(String.valueOf(a.getSignUpTime().getMonth()))<=7).
                count())).orElse(0);
        return new HomeStatisticsVo(incomeCompetition,topicCount,totality,headcount,yesterdaySum,weeksSum);
    }
}
